<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/17 9:26
 * Modified Time: 2019/7/17 9:26
 * Version: 2.0
 */

namespace App\Modules\Store\Criterias;


use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use Illuminate\Support\Facades\DB;

class GetStoreInfoCriteria extends BaseCriterias
{
    protected $store_id;

    public function __construct($store_id)
    {
        $this->store_id = $store_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;


        $member_id = FLAuth::id();;

        $store_like_query = DB::table('store_like')->selectRaw('count(*) as has_followed,member_id,store_id')
            ->where('member_id', $member_id)
            ->groupBy('store_id');


        $select = [
            'hx_store.id',
            'hx_store.name',
            'hx_picture.image as logo',
            'hx_store.phone',
            'hx_store.wechat',
            'hx_store.open_time',
            'hx_store.close_time',
            'hx_store.address',
            'hx_store.doorplate',
            'hx_store.order_sum',
            'hx_province.name as province',
            'hx_city.name as city',
            'hx_district.name as district',
//            '0 as has_followed',
            'hx_store.level',
            'IFNULL(hx_store_like.has_followed,0) as has_followed'
        ];

        $select = implode($select, ',');

        $model = $model->selectRaw($select)
            ->leftJoin('picture', 'store.logo', 'picture.id')
            ->leftJoin('order_comment', 'store.id', 'order_comment.store_id')
            ->leftJoin(\DB::raw("({$store_like_query->toSql()}) as hx_store_like"),function ($join) use($store_like_query){
                $join->mergeBindings($store_like_query)->on('store.id','=','store_like.store_id');
            })
            ->with(['storeResource' => function ($query) {
                $select = [
                    'hx_store_resource.id',
                    'hx_store_resource.store_id',
                    'hx_store_resource.resource',
                    'type',
                    'CASE WHEN type = 1 THEN hx_picture.image else hx_video.thumb END as image',
                    'CASE WHEN type = 1 THEN null else hx_video.url END as video',
                ];
                $select = implode($select, ',');

                $query->selectRaw($select)
                    ->leftJoin('picture', function ($join) {
                        $join->on('store_resource.resource', '=', 'picture.id');

                    })
                    ->leftJoin('video', function ($join) {
                        $join->on('store_resource.resource', '=', 'video.id');

                    });
            }])
	        ->leftJoin('city as district','store.adcode','code')
	        ->leftJoin('city','district.p_code','city.code')
	        ->leftJoin('city as province','city.p_code','province.code')
            ->where('store.id', $store_id);

        return $model;
    }
}